﻿/*
 * Crée par SharpDevelop.
 * Utilisateur: ngr
 * Date: 23/02/2015
 * Heure: 15:14
 * 
 * Pour changer ce modèle utiliser Outils | Options | Codage | Editer les en-têtes standards.
 */
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.IO;

namespace ComptaPerso.Controls
{
	/// <summary>
	/// Description of UC_BaseSelector.
	/// </summary>
	public partial class UC_BaseSelector : UserControl
	{
		List<string> ListFiles;
		string recentfiles;
		MainForm m;
		public UC_BaseSelector(MainForm m)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			this.m = m;
			ListFiles = new List<string>();
			recentfiles = Application.StartupPath + @"\recent.txt";
			LoadFiles();
			AfficherFichiers();
			
		}
	
		public void LoadFiles()
		{
			
			string[] lines = File.ReadAllLines(recentfiles);
			
			ListFiles.Clear();
			
			foreach (string s in lines)
			{
				if (!s.Equals(""))
				{
					ListFiles.Add(s);
				}
				
			}
			
		}
		
		public void AfficherFichiers()
		{
			LvwFile.Items.Clear();
			
			foreach (string s in ListFiles)
			{
				string[] tsplit = s.Split('\\');
				
				string zFile = tsplit[tsplit.Length - 1];
				
				ListViewItem lvi = new ListViewItem(zFile);
				lvi.SubItems.Add(s);
				
				LvwFile.Items.Add(lvi);
				
			}
		}
		
		public void AddFile(string filename)
		{
			if (!ListFiles.Contains(filename))
			{
				ListFiles.Add(filename);
			}
			
		}
		
		
		public void SaveOpenedFile()
		{
			File.WriteAllLines(recentfiles , ListFiles.ToArray());
			
		}
		
		void UC_BaseSelectorLoad(object sender, EventArgs e)
		{
			AfficherFichiers();
		}
		
		void LvwFileItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
		{
		//	MessageBox.Show("item selectionné");
		}
		
		void LvwFileMouseDoubleClick(object sender, MouseEventArgs e)
		{
			ListViewItem lvi = LvwFile.SelectedItems[0];
			
			//MessageBox.Show(lvi.SubItems[0].Text +  "  (" + lvi.SubItems[1].Text + ")");
			
			m.OpenDB(lvi.SubItems[1].Text);
		}
	}
}
